package com.sikaryofficial.backend.loader;

import com.sikaryofficial.backend.annotation.TenantFilter;
import com.sikaryofficial.backend.service.address.AddressCacheService;
import com.sikaryofficial.backend.service.address.CountryCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @author : qinjinyuan
 * @desc : 预热加载地址数据
 * @date : 2023/11/28 8:59
 */
@Component
@Slf4j
public class AddressLoader implements CommandLineRunner {
    @Autowired
    private AddressCacheService addressCacheService;
    @Autowired
    private CountryCacheService countryCacheService;
    @Value("${hismk.backend.loader.address}")
    private boolean loadFlag;

    @Value("${hismk.backend.loader.countryCode}")
    private boolean countryCodeLoadFlag;

    @Override
    @TenantFilter
    public void run(String... args) {
        if (Boolean.TRUE.equals(loadFlag)) {
            long start = System.currentTimeMillis();
            log.info("start: 地址数据预热=================================");
            addressCacheService.loadAllAddress();
            log.info("end: 地址数据预热=================================cost:{}", System.currentTimeMillis() - start);
            log.info("start: 国家code数据预热=================================");
            start = System.currentTimeMillis();
            countryCacheService.loadAllCountry();
            log.info("end: 国家code数据预热=================================cost:{}", System.currentTimeMillis() - start);
        }

        if (Boolean.TRUE.equals(countryCodeLoadFlag)) {
            long start = System.currentTimeMillis();
            log.info("start: 国家2位编码数据预热=================================");
            countryCacheService.loadAllCountryCode();
            log.info("end: 国家2位编码数据预热=================================cost:{}", System.currentTimeMillis() - start);
        }
    }
}
